package com.zmn.plat.business.impl.orderconfig.track.reason;

import java.util.*;
import javax.annotation.Resource;
import com.google.api.client.util.Sets;
import com.google.common.collect.Lists;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.plat.model.vo.orderconfig.track.OrderTrackVO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.string.InitialUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.business.interfaces.orderconfig.track.reason.OrderTrackReasonBService;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.orderconfig.track.OrderTrackReasonDRO;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.entity.orderconfig.track.OrderTrack;
import com.zmn.plat.model.entity.orderconfig.track.reason.OrderTrackReason;
import com.zmn.plat.services.interfaces.orderconfig.track.OrderTrackService;
import com.zmn.plat.services.interfaces.orderconfig.track.reason.OrderTrackReasonService;
import com.zmn.plat.utils.bean.BeanCopyUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * @author tanbiao
 * @description
 * @modifier
 * @since 2020/12/1 18:13
 **/
@Service
@Slf4j
public class OrderTrackReasonBServiceImpl implements OrderTrackReasonBService {
    @Resource
    private OrderTrackReasonService orderTrackReasonService;
    @Resource
    private OrderTrackService orderTrackService;

    @Override
    public List<OrderTrackReasonDRO> listByTrackId(Integer trackId) {
        if (NumberUtil.isNullOrZero(trackId)) {
            log.error("trackId 为空");
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        OrderTrack track = orderTrackService.findByKey(trackId);
        if (Objects.isNull(track) || StringUtil.isBlank(track.getTrackReason())) {
            log.error("track 为空或者track未配置缘由");
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }

        List<Integer> reasonIds = StringUtil.convertStrsToList(track.getTrackReason());
        List<OrderTrackReason> reasonList = orderTrackReasonService.listByKeys(reasonIds);

        if (CollectionUtils.isEmpty(reasonList)) {
            log.error("配置内容中配置的reasonIds未查询到对应的数据");
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }

        List<OrderTrackReasonDRO> droList = BeanCopyUtils.copyProperties(reasonList, OrderTrackReason.class, OrderTrackReasonDRO.class, OrderTrackReasonDRO::new);

        return droList;
    }

    @Override
    public List<OrderTrackReasonDRO> listByTrackIds(List<Integer> trackIdList) {
        if (CollectionUtil.isNullOrEmpty(trackIdList)) {
            log.error("trackIdList 为空");
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }
        List<OrderTrackVO> orderTrackVOS = orderTrackService.listByKeys(trackIdList);
        Set<Integer> reasonIds = Sets.newHashSet();
        orderTrackVOS.forEach(orderTrackVO -> {
            String trackReasonStr = orderTrackVO.getTrackReason();
            if (StringUtil.isBlank(trackReasonStr)) {
                return;
            }
            reasonIds.addAll(StringUtil.convertStrsToList(trackReasonStr));
        });

        if (CollectionUtil.isNullOrEmpty(reasonIds)) {
            log.error("track 为空或者track未配置缘由");
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }
        List<OrderTrackReason> reasonList = orderTrackReasonService.listByKeys(Lists.newArrayList(reasonIds));

        if (CollectionUtils.isEmpty(reasonList)) {
            log.error("配置内容中配置的reasonIds未查询到对应的数据");
            throw new PlatException(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }

        List<OrderTrackReasonDRO> droList = BeanCopyUtils.copyProperties(reasonList, OrderTrackReason.class, OrderTrackReasonDRO.class, OrderTrackReasonDRO::new);

        return droList;
    }

    @Override
    public Integer addEdit(OrderTrackReason orderTrackReason, String operator) {
        Date now = DateUtil.getNow();

        OrderTrackReason reason = orderTrackReasonService.findByReason(orderTrackReason.getReason());
        boolean isExist = Objects.nonNull(reason);
        // 新增
        if (Objects.isNull(orderTrackReason.getReasonId())) {
            if (isExist) {
                throw new PlatException("缘由不能重复");
            }
            orderTrackReason.setUpdateTime(now);
            orderTrackReason.setUpdater(operator);
            orderTrackReason.setFirstLetter(InitialUtil.getFristEnName(orderTrackReason.getReason()));
            orderTrackReason.setCreateTime(now);
            orderTrackReason.setCreater(operator);
            orderTrackReason.setStatus(GlobalConsts.YES);
            orderTrackReasonService.insert(orderTrackReason);
        } else {
            if (isExist && !Objects.equals(reason.getReasonId(), orderTrackReason.getReasonId())) {
                throw new PlatException("名称不能重复");
            }
            OrderTrackReason old = orderTrackReasonService.findByKey(orderTrackReason.getReasonId());
            old.setFirstLetter(InitialUtil.getFristEnName(orderTrackReason.getReason()));
            old.setUpdateTime(now);
            old.setUpdater(operator);
            old.setReason(orderTrackReason.getReason());
            old.setSort(orderTrackReason.getSort());
            orderTrackReasonService.updateByKey(old);
        }
        return GlobalConsts.YES;
    }
}
